﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlsLib;
using FrameworkLib.Configurations.FormsConfiguration.BL;


namespace FrameworkLib.Configurations.FormsConfiguration
{
    public partial class AnagraficaConfigurazioneMaschere : AnagraficaConfigurazioneMaschereBase
    {
        public AnagraficaConfigurazioneMaschere()
        {
            InitializeComponent();
        }

        public override void InitializeForm()
        {
            base.InitializeForm();

            LoadData(false);

            //this.InitializeButtonBar(new bool[]{false, false, true, true, true});

            this.EnableSorting = true;
            this.splitContainer1.Panel2Collapsed = true;
            this.ShowPanelMessage = false;
            this.CanModify = false;
            this.ButtonClicked += new EventHandler<CustomEventArgs<FormConfiguration>>(Anagrafica_ButtonClicked);
            this.ItemSelected += new EventHandler<CustomEventArgs<FormConfiguration>>(Anagrafica_ItemSelected);

        }

        void Anagrafica_ButtonClicked(object sender, CustomEventArgs<FormConfiguration> e)
        {
            FrameworkLib.Configurations.FormsConfiguration.Facade.FormsConfigurationFacade fcf = new Facade.FormsConfigurationFacade();

            switch (e.Action)
            {
                case Actions.New:
                    base.ClearControls(this.Controls);
                    break;
                case Actions.Add:
                    {
                        MessageBox.Show("Funzionalità non attiva.", "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    break;
                case Actions.Modify:
                    {
                        MessageBox.Show("Funzionalità non attiva.", "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    break;
                case Actions.Delete:
                    {
                        fcf.Delete(base.Current.ID);

                        LoadData(false);
                    }
                    break;
                default:
                    break;
            }
        }

        void Anagrafica_ItemSelected(object sender, CustomEventArgs<FormConfiguration> e)
        {
            base.Current = e.Item;
        }

        private void LoadData(bool selectLast)
        {
            FrameworkLib.Configurations.FormsConfiguration.Facade.FormsConfigurationFacade pf = new FrameworkLib.Configurations.FormsConfiguration.Facade.FormsConfigurationFacade();

            pf.RunAsync<List<FormConfiguration>>(() => pf.GetForms(), (result) =>
            {
                this.Datasource = result;

                foreach (DataGridViewColumn c in this.dataGridView1.Columns) c.SortMode = DataGridViewColumnSortMode.Automatic;
            });

            this.SetGridItemSelected(0);
        }
    }

    public class AnagraficaConfigurazioneMaschereBase : AnagraficaManager<FormConfiguration> { }
}
